package com.jhjx.wx.mp.handler;

import com.alibaba.fastjson.JSONObject;
import com.jhjx.wx.mp.utils.HttpClientUtils;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 取消关注事件处理
 */
@Component
public class UnsubscribeHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        String openId = wxMessage.getFromUser();
        this.logger.info("取消关注用户 OPENID: " + openId);
        // 更新本地数据库为取消关注状态
        JSONObject json = new JSONObject();
        json.put("careFlag","0");
        json.put("openId",openId);
        JSONObject result =  HttpClientUtils.httpPost("http://127.0.0.1:8081/api/shop/updateCareFlagByOpenId",json);
        if ("-1".equals(result.get("code"))) {
            this.logger.error("取消关注发生异常！");
        }
        return null;
    }

}
